package com.woniuxy.java106crm.quartz;

import com.woniuxy.java106crm.entity.Lost;
import com.woniuxy.java106crm.service.LostService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Slf4j
public class CustomerAlertJob implements Job {
    @Resource
    private LostService lostService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        // 在这里编写客户检查的逻辑
        System.out.println("Running customer alert job at: " + new Date());
        // 调用之前的客户检查逻辑代码
        performCustomerCheck();
    }

    private void performCustomerCheck() {
        //创建一个list暂存大于六个月没有购买的的客户
        ArrayList<Lost> list = new ArrayList<>();
        List<Lost> all = lostService.findAll();
        for(Lost a : all){
            //如果最近一次购买时间大于六个月，存入list
            if (a.getLasttime().plusMonths(6).isBefore(LocalDateTime.now())){
                list.add(a);
            }
        }
        List<Lost> allLost = lostService.findAllLost();
        //移除lost表已存在的客户信息
        list.removeAll(allLost);
        //遍历list将lost表中没有的客户存入
        for(Lost lost : list){
            lostService.addLost(lost);
        }
    }
}